<template>
  <div class="login-container">
    <div class="left" />
    <div class="right">
      <div div="box">
        <h3>登录</h3>
        <el-form ref="ruleForm" :model="ruleForm" :rules="rules" label-width="100px" class="demo-ruleForm" @validate="validateFn">
          <el-form-item label="用户名" prop="mobile">
            <el-input v-model="ruleForm.mobile" />
          </el-form-item>
          <el-form-item label="密码" prop="password" show-password>
            <el-input v-model="ruleForm.password" />
          </el-form-item>
          <el-form-item prop="isAgree">
            <el-checkbox v-model="ruleForm.isAgree">同意使用协议</el-checkbox>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submitForm()">登录</el-button>
            <el-button>取消</el-button>
          </el-form-item>
        </el-form></div>
    </div>
  </div>
</template>

<script>

export default {
  name: '',
  data() {
    var validateIsArray = (rule, value, callback) => {
      // if (!value) {
      //   callback(new Error('请勾选用户协议'))
      // } else {
      //   callback()
      // }
      // 下面代码： 先保证代码成功，最后再去玩技巧，优化代码
      value ? callback() : callback(new Error('请勾选用户协议'))
    }
    return {
      onSubmit: {},
      ruleForm: {
        mobile: process.env.NODE_ENV === 'development' ? '13800000002' : '',
        password: '123456',
        isAgree: false
      },
      rules: {
        mobile: [
          { required: true, message: '请输入手机号', trigger: 'blur' },
          {
            pattern: /^1[34578]\d{9}$/, // 正则校验不用字符串
            message: '请填写正确的手机号码', trigger: 'blur'
          }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 20, message: '密码位数不对' }
        ],
        isAgree: [
          { 'validator': validateIsArray, 'trigger': 'change' }
        ]

      }
    }
  },
  methods: {
    validateFn() {
      // console.log(123)
    },
    submitForm() {
      this.$refs.ruleForm.validate(async(valid) => {
        if (valid) {
          /* sysLoginRequest({
            mobile: this.ruleForm.mobile,
            password: this.ruleForm.password
          })
            .then(function(response) {
              console.log(response)
            })
            .catch(function(error) {
              console.log(error)
            }) */
          try {
            this.$store.dispatch('user/loginAction', {
              mobile: this.ruleForm.mobile,
              password: this.ruleForm.password
            })
          } catch (error) {
            //
          }
        } else {
          // console.log('error submit!!')
          return false
        }
      })
    }
  }
}

</script>

<style lang="scss">
.login-container{
  background-color: aqua;
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: row;
}
.left{
  flex:3;
  background-image: url('~@/assets/common/login_back.png');
  background-size: cover;
  background-repeat: no-repeat;

}
.right{
  flex: 2;
  display: flex;
  justify-content: center;
  align-items: center;
  .box{
    width: 100px

  }
  .el-input__inner{
      width: 300px;
    }
  h3{
    margin-left: 35px;
  }
}
</style>
